#include "CfgInfo.h"
#include<PluginException.h>
//#include <mutex>
#include"HikDVRSDKErrorMsg.h"

//typedef void(CALLBACK* fVoiceDataCallBack)(
//	LONG     lVoiceComHandle,
//	char* pRecvDataBuffer,
//	DWORD    dwBufSize,
//	BYTE     byAudioFlag,
//	void* pUser
//	);


string CfgInfo::lastErrorMsg;



RESULT_CODE CfgInfo::getLastErrorMsg(CameraDevInfo devInfo, string& outputMsg)
{
	outputMsg= CfgInfo::lastErrorMsg;
	return RESULT_CODE::RET_OK;
}

RESULT_CODE CfgInfo::getChannelsInfo(CameraDevInfo devInfo, Channels& channels)
{
	try
	{
		CDevice* pDev = getDevice(devInfo);
		string resultMsg;
		if (pDev == nullptr) {
			return RESULT_CODE::RET_FAILED;
		}
		
		int getCnt = 0;
		for (int i = 0; i < 64; i++) {
			if (getCnt >= 100) {
				break;
			}
			if (pDev->deviceInfo.struChanInfo[i].bEnable) {
				channels.channelArray[getCnt].channelNumber = pDev->deviceInfo.struChanInfo[i].iChanIndex;
				channels.channelArray[getCnt].isEnable = true;
				channels.channelArray[getCnt].name = pDev->deviceInfo.struChanInfo[i].chChanName;
				getCnt++;
			}
		}
		channels.cnt = getCnt;
		return RESULT_CODE::RET_OK;
	}
	catch (PluginException& eException) {
		CfgInfo::lastErrorMsg = eException.GetExcepInfo();
		return RESULT_CODE::RET_FAILED;
	}
	return RESULT_CODE::RET_OK;
}
RESULT_CODE CfgInfo::getChannelPresets(CameraDevInfo devInfo, long channleId, Presets& presets)
{
	try
	{
		CDevice* pDev = getDevice(devInfo);
		string resultMsg;
		if (pDev == nullptr) {
			return RESULT_CODE::RET_FAILED;
		}

		//memset(&picCfg, 0, sizeof(picCfg));
		DWORD  dwReturned = 0;
		NET_DVR_PRESET_NAME presetNameArray[300] = { 0 };
		bool bRet = NET_DVR_GetDVRConfig(pDev->m_lLoginID, NET_DVR_GET_PRESET_NAME, channleId, &presetNameArray, sizeof(presetNameArray), &dwReturned);
		if (!bRet) {
			throw PluginException(GetDVRNETLastErrorMsg(NET_DVR_GetLastError()));
		}
		int numberCnt = dwReturned / sizeof(NET_DVR_PRESET_NAME);
		for (int i = 0; i < numberCnt; i++) {
			presets.presetArray[i].name = presetNameArray[i].byName;
			presets.presetArray[i].number = presetNameArray[i].wPresetNum;
		}
		presets.cnt = numberCnt;
		return RESULT_CODE::RET_OK;
	}
	catch (PluginException& eException) {
		CfgInfo::lastErrorMsg = eException.GetExcepInfo();
		return RESULT_CODE::RET_FAILED;
	}
	return RESULT_CODE::RET_OK;
}
CDevice* CfgInfo::getDevice(CameraDevInfo devInfo)
{
	CDevice* pDev = CDeviceManage::getInstance()->GetDeviceByUUID(devInfo.devId);
	string resultMsg;
	if (pDev == nullptr) {
		DeviceInfo contentDeviceInfo;
		sprintf_s(contentDeviceInfo.strUUID, devInfo.devId.c_str());
		sprintf_s(contentDeviceInfo.strIP, devInfo.devIp.c_str());
		contentDeviceInfo.nPort = devInfo.devPort;
		sprintf_s(contentDeviceInfo.strUserName, devInfo.devUsername.c_str());
		sprintf_s(contentDeviceInfo.strPassword, devInfo.devPassword.c_str());
		pDev = CDeviceManage::getInstance()->GetDeviceByDevInfo(contentDeviceInfo, resultMsg, true);
		if (pDev == nullptr) {
			CfgInfo::lastErrorMsg = resultMsg;
		}
	}
	return pDev;
}
